package com.liy.framework.common.utils;

import java.text.*;
import java.util.*;

public class DateUtil
{
    private static String defaultDatePattern;
    public static final String YMD = "yyyy-MM-dd";
    public static final String YMDHMS = "yyyy-MM-dd HH:mm:ss";
    
    public static String getDatePattern() {
        return DateUtil.defaultDatePattern;
    }
    
    public static Date getNowDate() {
        final Date currentTime = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String dateString = formatter.format(currentTime);
        final ParsePosition pos = new ParsePosition(8);
        final Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }
    
    public static String getDayDateString(final int day) {
        return getDayDateString(day, "yyyy-MM-dd");
    }
    
    public static String getDayDateString(final int day, final String formatStyle) {
        final SimpleDateFormat sdf = new SimpleDateFormat(formatStyle);
        final Date today = new Date();
        final Calendar theCa = Calendar.getInstance();
        theCa.setTime(today);
        theCa.add(5, day);
        final Date start = theCa.getTime();
        final String startDate = sdf.format(start);
        return startDate;
    }
    
    public static Date nowdate() {
        final DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        final DateFormat format2 = new SimpleDateFormat("yyyy\u5e74MM\u6708dd\u65e5 HH\u65f6mm\u5206ss\u79d2");
        Date date = null;
        String str = null;
        str = "2007-1-18";
        try {
            date = format1.parse(str);
            date = format2.parse(str);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }
    
    public static String getTimeNow() {
        final Date currentTime = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String dateString = formatter.format(currentTime);
        return dateString;
    }
    
    public static String getTimeNow(final String formatStyle) {
        final Date currentTime = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat(formatStyle);
        final String dateString = formatter.format(currentTime);
        return dateString;
    }
    
    public static String getStringDateShort() {
        final Date currentTime = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        final String dateString = formatter.format(currentTime);
        return dateString;
    }
    
    public static String getTimeShort() {
        final SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        final Date currentTime = new Date();
        final String dateString = formatter.format(currentTime);
        return dateString;
    }
    
    public static String formatDate(final String dateStr) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date date = formatter.parse(dateStr);
        formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        return formatter.format(date);
    }
    
    public static Date strToDateLong(final String strDate) {
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final ParsePosition pos = new ParsePosition(0);
        final Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
    
    public static String dateToStrLong(final Date dateDate) {
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String dateString = formatter.format(dateDate);
        return dateString;
    }
    
    public static String dateToStr(final Date dateDate) {
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        final String dateString = formatter.format(dateDate);
        return dateString;
    }
    
    public static String dateToString(final Date dateDate) {
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        final String dateString = formatter.format(dateDate);
        return dateString;
    }
    
    public static Date strToDate(final String strDate) {
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        final ParsePosition pos = new ParsePosition(0);
        final Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }
    
    public static Date strToDateTime(final String strDate) throws ParseException {
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.parse(strDate);
    }
    
    public static Date getNow() {
        final Date currentTime = new Date();
        return currentTime;
    }
    
    public static Date getLastDate(final long day) {
        final Date date = new Date();
        final long date_3_hm = date.getTime() - 122400000L * day;
        final Date date_3_hm_date = new Date(date_3_hm);
        return date_3_hm_date;
    }
    
    public static String getStringToday() {
        final Date currentTime = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
        final String dateString = formatter.format(currentTime);
        return dateString;
    }
    
    public static String getHour() {
        final Date currentTime = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String dateString = formatter.format(currentTime);
        final String hour = dateString.substring(11, 13);
        return hour;
    }
    
    public static String getTime() {
        final Date currentTime = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final String dateString = formatter.format(currentTime);
        final String min = dateString.substring(14, 16);
        return min;
    }
    
    public static String getFormatDate(final String sformat) {
        final Date currentTime = new Date();
        final SimpleDateFormat formatter = new SimpleDateFormat(sformat);
        final String dateString = formatter.format(currentTime);
        return dateString;
    }
    
    public static List<?> getYearList() {
        final Calendar cal = Calendar.getInstance();
        final int nowyear = cal.get(1);
        final List<Integer> yearlist = new ArrayList<Integer>();
        for (int i = nowyear - 5; i < nowyear + 6; ++i) {
            yearlist.add(i);
        }
        return yearlist;
    }
    
    public static int getYear() {
        final Calendar cal = Calendar.getInstance();
        return cal.get(1);
    }
    
    public static String stampToDate(final String s) {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final long lt = new Long(s);
        final Date date = new Date(lt);
        final String res = simpleDateFormat.format(date);
        return res;
    }
    
    public static String dateToStamp(final String s) throws ParseException {
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        final Date date = simpleDateFormat.parse(s);
        final long ts = date.getTime();
        final String res = String.valueOf(ts);
        return res;
    }
    
    public static void main(final String[] args) {
        System.out.println(getDayDateString(-30, "yyyyMMdd000000"));
        try {
            System.out.println(new SimpleDateFormat("yyyyMMddHHmmss").parse("20000101000000"));
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(stampToDate(System.currentTimeMillis() + ""));
    }
    
    static {
        DateUtil.defaultDatePattern = "yyyy-MM-dd ";
    }
}
